package com.nhncorp.pleiades.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import com.nhncorp.pleiades.conf.PleiadesConfiguration;

public class Resource extends ShellCommand {
  
  public static final long DF_INTERVAL_DEFAULT = 3 * 1000; // default DF refresh interval 
  
  private String  dirPath;
  private String filesystem;
  private long capacity;
  private long used;
  private long available;
  private int percentUsed;
  private String mount;
  
  public Resource(File path, PleiadesConfiguration conf) throws IOException {
    this(path, conf.getLong("dfs.df.interval", Resource.DF_INTERVAL_DEFAULT));
  }

  public Resource(File path, long dfInterval) throws IOException {
    super(dfInterval);
    this.dirPath = path.getCanonicalPath();
  }

  public String getDirPath() {
    return dirPath;
  }
  
  public String getFilesystem() throws IOException { 
    run(); 
    return filesystem; 
  }
  
  public long getCapacity() throws IOException { 
    run(); 
    return capacity; 
  }
  
  public long getUsed() throws IOException { 
    run(); 
    return used;
  }
  
  public long getAvailable() throws IOException { 
    run(); 
    return available;
  }
  
  public int getPercentUsed() throws IOException {
    run();
    return percentUsed;
  }

  public String getMount() throws IOException {
    run();
    return mount;
  }
  
  public String toString() {
    return
      "df -k " + mount +"\n" +
      filesystem + "\t" +
      capacity / 1024 + "\t" +
      used / 1024 + "\t" +
      available / 1024 + "\t" +
      percentUsed + "%\t" +
      mount;
  }

  protected String[] getExecString() {
    return new String[] {"df","-k", dirPath};
  }
  
  protected void parseExecResult(BufferedReader lines) throws IOException {
    lines.readLine();                         // skip headings
  
    String line = lines.readLine();
    if (line == null) {
      throw new IOException( "Expecting a line not the end of stream" );
    }
    StringTokenizer tokens =
      new StringTokenizer(line, " \t\n\r\f%");
    
    this.filesystem = tokens.nextToken();
    if (!tokens.hasMoreTokens()) {            // for long filesystem name
      line = lines.readLine();
      if (line == null) {
        throw new IOException( "Expecting a line not the end of stream" );
      }
      tokens = new StringTokenizer(line, " \t\n\r\f%");
    }
    this.capacity = Long.parseLong(tokens.nextToken()) * 1024;
    this.used = Long.parseLong(tokens.nextToken()) * 1024;
    this.available = Long.parseLong(tokens.nextToken()) * 1024;
    this.percentUsed = Integer.parseInt(tokens.nextToken());
    this.mount = tokens.nextToken();
  }

  public static void main(String[] args) throws Exception {
    String path = ".";
    if (args.length > 0)
      path = args[0];

    System.out.println(new Resource(new File(path), DF_INTERVAL_DEFAULT).toString());
  }

}
